package com.eeepay.eeepay_v2.api;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import cn.eeepay.api.grpc.nano.ResMsgProto;
import cn.eeepay.api.grpc.nano.SuperPushServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：获取超级推分享开关状态
 * 作者：zhuangzeqin
 * 时间: 2017/8/11-9:43
 * 邮箱：zzq@eeepay.cn
 */
public class GetSuperPushShareStatusAPI {
    private ResultCallBack resultCallBack;//设置监听接口

    /**
     * 设置监听接口
     *
     * @param resultCallBack
     */
    public void setResultCallBack(ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 获取超级推分享开关状态
     */
    public void getSuperPushShareStatusInfo()
    {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, 0, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                SuperPushServiceGrpc.SuperPushServiceBlockingStub stub =
                        SuperPushServiceGrpc.newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                ManagementCenterProto.UserInfoReq userInfoReq = new ManagementCenterProto.UserInfoReq();
                userInfoReq.userId = UserInfo.getUserInfo2SP().getAgentNo();
                return stub.getSuperPushShareStatus(userInfoReq);
            }

            @Override
            public void updateUI(Object result, int taskId) {
                if (result == null) {
                    return;
                }
                ResMsgProto.ResMsg resMsg = (ResMsgProto.ResMsg) result;
                resultCallBack.onSucess(resMsg.status);
            }
        });


    }
    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {

        void onSucess(boolean msg);//成功返回true or false 代表开关是否关闭
    }
}
